package com.hzit.servlet.emp;

import com.hzit.dao.EmpDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DeleteEmpServlet", urlPatterns = "/emp/delete")
public class DeleteEmpServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //2.获取客户端传递过来的参数
        String empno = request.getParameter("empno");

        //如果存在转为 int,否则默认:-1 （随便区）
        int id = empno != null ? Integer.parseInt(empno) : -1;

        //3.操作数据库
        EmpDao empDao = new EmpDao();
        int row = empDao.deleteByPrimary(id);
        System.out.println("删除影响行数:" + row);

        //4.重新查询一下最新的数据  重定向
        String contextPath = request.getContextPath(); //动态获取项目名 /web_manage

        response.sendRedirect("/web_manage/emp/all");

    }
}
